<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript">
	var delGradenIds=new Array();//删除id集合
	$(function() {
		getMaintainMsg();
		mySubmit("gardenMainForm", initgardenMainFormData, gardenMainSucess);
	});
	//获取编辑页面数据
	function getMaintainMsg() {
		var fkProjectid = parent.parent.parent.projectID; //建筑id
		var url = "assortInfo/getMaintainByBuild";
		$.getJSON(url, {
			fkProjectId : fkProjectid
		}, function(data, status) {
			if (data != null && data.length>0) {
				var html = "<tr><td colspan=\"6\"><a class=\"btn btn-danger radius\" id=\"addGarden\" onclick=\"addGardenTr(this)\"> 添加</a></td></tr>";
					for (var i=0;i<data.length;i++) {
						html += "<tr><td width=\"40\"><span>位置</span></td><td><input type=\"type\" id=\"\" name=\"position\" value=\""+data[i].position+"\" /></td>"+
						"<td width=\"40\"><span>朝向</span></td><td ><input type=\"type\" id=\"\" name=\"orientation\" value=\""+data[i].orientation+"\" />"+
						"<td width=\"40\"><span>材料</span></td><td><input name=\"gardenId\" id=\"entityId_id\" value=\""+data[i].id+"\" hidden=\"hidden\"><input type=\"type\" id=\"\" name=\"materials\" value=\""+data[i].materials+"\" />"+
						"</td>\<td width=\"40\"><span>高度</span></td>\<td ><input type=\"type\" id=\"\" name=\"height\" value=\""+returnFloat(data[i].height)+"\" /> m</td>"+
						"<td width=\"40\"><span>长度</span></td><td><input type=\"type\" id=\"\" name=\"length\" value=\""+returnFloat(data[i].length)+"\" /> m "+
						"<a data-id=\""+data[i].id+"\" href=\"javascript:void(0)\"onclick=\"delGardenTr(this)\" name=\"delBtn\"><i class=\"icon-fullscreen\"></i></a></td></tr>";
					}
				$("#gardenMainForm").find("#gmtn").html(html);
				$("#gardenMainForm").find("#btn_div2").show();
				$("#gardenMainForm").find("#btn_div1").hide();
				$("#gardenMainForm").find("input,textarea").attr("readonly", true);
				$("#gardenMainForm").find("#addGarden").attr("disabled",true);
				$("#gardenMainForm").find("[name='delBtn']").hide();
			} else {
				$("#gardenMainForm").find("#btn_div2").hide();
				$("#gardenMainForm").find("#btn_div1").show();
			}
		});
	}

	var optMaintain;
	//optMaintain操作字段，0：临时保存；1：新增；2：修改
	function saveMaintain(val) {
		optMaintain = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#gardenMainForm").find("input,textarea").attr("readonly", false);
				$("#gardenMainForm").find("#modifyBtn").hide();
				// $(".hidbtn").removeClass("hidbtn");
				$("#gardenMainForm").find(".hidbtn").addClass("showbtn");
				$("#gardenMainForm").find("#addGarden").attr("disabled",false);
				$("#gardenMainForm").find("[name='delBtn']").show();
				layer.close(index);
			});
		} else {

			$("#gardenMainForm").submit();
		
		}
	}
	var x;//load弹窗index
	//初始化表单数据
	function initgardenMainFormData() {
		x=layer.load(2);
		var gardenList = new Array();
		var $gardens = $("#gardenMainForm #gmtn tr:gt(0)");
		var fkProjectid = parent.parent.parent.projectID; //建筑id
		for(var i=0;i<$gardens.length;i++){
			var v=$gardens[i]
			var id = $(v).find("input[name='gardenId']").val();
			var materials = $(v).find("input[name='materials']").val();
			var height = $(v).find("input[name='height']").val();
			var length = $(v).find("input[name='length']").val();
			var orientation = $(v).find("input[name='orientation']").val();
			var position = $(v).find("input[name='position']").val();
			if (!testNum(height)) {
				layer.msg("高度请输入数值");
				$(v).find("input[name='height']").focus();
				layer.close(x);
				return null;
			}else if (!testNum(length)) {
				layer.msg("长度请输入数值");
				$(v).find("input[name='length']").focus();
				layer.close(x);
				return null;
			}else{
				gardenList.push({
					id : id,
					materials : materials,
					height : height,
					length : length,
					fkProjectid : fkProjectid,
					orientation:orientation,
					position:position
				});
			}
		};
		if(gardenList.length==0){
			return null;
		}else{
			return {
				gardenList:gardenList,
				delGradenIds:delGradenIds,
				opt : optMaintain
			};
		}

	}
	//删除
	function delGardenTr(obj) {
			var aId = $(obj).attr("data-id");
			console.log(aId);
			delGradenIds.push(aId);
			$(obj).parent().parent().remove();
			layer.msg("点击保存后，将彻底删除该条数据！");
		}
	//提交后的回调函数(就是ajax的回调函数)
	function gardenMainSucess(responseText) {
		if (responseText.data) {
				layer.alert("保存成功！",{
				offset:'20%',
				shade:[0.1,'#fff']
			},function(){
				getMaintainMsg();
				$("#gardenMainForm").find("#modifyBtn").show();
				$("#gardenMainForm").find(".hidbtn").removeClass("showbtn");
				layer.closeAll();
			});
				layer.close(x);
				
			} else {
				layer.alert('保存失败！');
				layer.close(x);
			}
	}
	//添加
	
	function addGardenTr(obj) {
		var html ="<tr><td width=\"40\"><span>位置</span></td><td><input type=\"type\" id=\"\" name=\"position\" value=\"\" /></td>"+
			"<td width=\"40\"><span>朝向</span></td><td ><input type=\"type\" id=\"\" name=\"orientation\" value=\"\" /> </td>"+
			"<td width=\"40\"><span>材料</span></td><td><input name=\"gardenId\" value=\"\" hidden=\"hidden\"><input type=\"type\" id=\"\" name=\"materials\" value=\"\" />"+
			"</td>\<td width=\"40\"><span>高度</span></td>\<td ><input type=\"type\" id=\"\" name=\"height\" value=\"\" /> m</td>"+
			"<td width=\"40\"><span>长度</span></td><td><input type=\"type\" id=\"\" name=\"length\" value=\"\" />m "+
			"<a href=\"javascript:void(0)\"onclick=\"delGardenTr(this)\" name=\"delBtn\"><i class=\"icon-fullscreen\"></i></a></td></tr>";
		$(obj).parent().parent().parent().append(html);
	}
</script>
<style>
#gardenMainForm input{
	width:125px;
}
</style>
<!-- 园区围护 -->
<form class="cmxform" id="gardenMainForm" method="post"
	action="assortInfo/addGardenMaintain">
	<div class="panel panel-default">
		<div class="panel-heading">
			<h5 class="panel-title">园区围护</h5>
		</div>
		<div class="panel-body">

			<div class="style_box">
				<table cellpadding="10" cellspacing="5" width="100%" id="gmtn">
					<tr>
						<td colspan="10"><a class="btn btn-danger radius"
							id="addGarden" onclick="addGardenTr(this)"> 添加</a></td>
					</tr>

				</table>
			</div>
		</div>
	</div>
	<input name="id" id="entityId_gmId" hidden="hidden">
	<div style="width:100%;text-align:center; padding-top:20px">
		<div id="btn_div1">
			<button class="btn btn-danger radius my-btn"
				onclick="saveMaintain(0)" type="button">提交</button>
			<button class="btn btn-danger radius my-btn"
				onclick="saveMaintain(1)" type="button">保 存</button>
			<a class="btn btn-default radius"
				onClick="resetForms('gardenMainForm');"> 重置 </a> <br />
		</div>
		<div id="btn_div2" style="display:none;">
			<shiro:hasPermission name="permission:edit">
				<button id="modifyBtn" type="button"
					class="btn btn-danger radius my-btn" onclick="saveMaintain(3)">启用编辑</button>
				<button class="btn btn-danger radius my-btn hidbtn"
					onclick="saveMaintain(2)" type="button">保存修改</button>
				<a class="btn btn-default radius hidbtn"
					onClick="resetForms('gardenMainForm');">重置</a>
			</shiro:hasPermission>
		</div>
	</div>
</form>
